package com.example.aics.tools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.example.aics.dto.AppointMentDTO;
import com.example.aics.entity.Appointment;
import com.example.aics.service.AppointmentService;
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

@Component
public class AppointmentTools {


    @Resource
    private AppointmentService appointmentService;

    private static final Logger logger = LoggerFactory.getLogger(AppointmentTools.class);

    record AppointmentRecord(String userName,String idCard,String department,String date,String time,String doctorName){

    }


    @Tool(name = "bookAppointment",value = "根据参数先执行querDepartment，查询是否可预约，并直接给用户回答是否可预约" +
            "，并让用户确定所有的预约消息，用户确认后再进行预约。如果用户没有提供医生姓名，请从向量存储中随机找到一位医生")
    public String bookAppointment(AppointmentRecord appointmentRecord) {
        logger.info("bookAppointment预约参数：" + appointmentRecord);
        if(!checkAppointment(appointmentRecord)){
            return "请输入预约信息";
        }
        AppointMentDTO appointMentDTO = new AppointMentDTO();
        BeanUtils.copyProperties(appointmentRecord, appointMentDTO);
        Appointment appointment = appointmentService.findOne(appointMentDTO);
        if (appointment != null) {
            return "您在相同的科室和时间段都有预约了";
        }
        appointment = new Appointment();
        BeanUtils.copyProperties(appointmentRecord, appointment);
        boolean isSuccess = appointmentService.save( appointment) ;
        if (!isSuccess) {
            return "预约失败";
        }
        return "预约成功，并返回预约详情";
    }


    @Tool(name = "cancelAppointment",value = "在执行取消预约之前，必须提供详细的预约信息，并让用户确认所提供的预约信息，用户确认之后，才能执行取消预约")
    public String cancelAppointment(AppointmentRecord appointmentRecord) {
        logger.info("cancelAppointment取消预约参数：" + appointmentRecord);
        if(!checkAppointment(appointmentRecord)){
            return "请输入预约信息";
        }
        AppointMentDTO appointMentDTO = new AppointMentDTO();
        BeanUtils.copyProperties(appointmentRecord, appointMentDTO);
        Appointment appointment = appointmentService.findOne(appointMentDTO);
        if (appointment == null) {
            return "您没有此预约，请核对预约科室和时间";
        }
        boolean isSuccess = appointmentService.removeById(appointment.getId());
        if (!isSuccess) {
            return "取消预约失败";
        }
        return "取消预约成功";
    }

    @Tool(name = "querDepartment",value = "根据科室名称、日期和医生，查询是否有号源，并返回给用户")
    public boolean querDepartment(@P(value = "科室名称") String department
            ,@P(value = "日期") String date
            ,@P(value = "时间段，可选值：上午、下午") String time
            , @P(value = "医生",required = false) String doctorName) {
        logger.info("querDepartment  查询科室：" + department + "，日期：" + date + "，时间段：" + time + "，医生：" + doctorName);
        //TODO 维护医生的排班信息
        /**
         * 如果没有指定医生名字，则根据其他条件查询是否有可预约的医生 ，没有则返回false
         * 如果指定了医生名字，则判断这个医生有没有排班 ，没有则返回false
         * 如果有排班，则判断医生排班时间段是否已经约满，约满就返回false
         */
        return true;
    }

    /**
     * 检查预约条件
     */
    private boolean checkAppointment(AppointmentRecord appointmentRecord) {
        if(appointmentRecord == null){
            return false;
        }
        return true ;
    }
}
